#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_Image.h>
#include "constantes.h"
#include "fichiers.h"

extern SDL_Surface* screen;

int getMap(int map[])
{
    int i;
    FILE* input = fopen("Levels/custom.lvl", "r");
    if (!input)
        return 0;
    for (i = 0 ; i < NB_BLOCS ; i++)
        map[i] = fgetc(input) - '0';

    fclose(input);
    return 1;
}

int setMap(int map[])
{
    int i;
    FILE* output = fopen("Levels/custom.lvl", "w");

    if (!output)
        return 0;
    for (i = 0 ; i < NB_BLOCS ; i++)
        fputc(map[i] + '0', output);

    fclose(output);
    return 1;
}

void loadTiles(SDL_Surface* tiles[], SDL_Surface* marioTiles[])
{
    marioTiles[HAUT] = IMG_Load("images/mario_haut.gif");
    marioTiles[BAS] = IMG_Load("images/mario_bas.gif");
    marioTiles[GAUCHE] = IMG_Load("images/mario_gauche.gif");
    marioTiles[DROITE] = IMG_Load("images/mario_droite.gif");
    tiles[MUR] = IMG_Load("images/mur.PNG");
    tiles[CAISSE] = IMG_Load("images/caisse.PNG");
    tiles[OBJECTIF] = IMG_Load("images/objectif.PNG");
    tiles[CAISSE_OK] = IMG_Load("images/caisse_ok.PNG");
    tiles[MARIO] = marioTiles[BAS];
}

void displayMap(int map[], SDL_Surface* tiles[])
{
    int i;
    SDL_Rect current = {0};

    SDL_FillRect(screen, NULL, 16777215);
    for (i = 0 ; i < NB_BLOCS ; i++)
    {
        current.x = (i % NB_BLOCS_LARGEUR) * TAILLE_BLOC;
        current.y = i / NB_BLOCS_LARGEUR * TAILLE_BLOC;
        if (map[i] != VIDE)
            SDL_BlitSurface(tiles[map[i]], NULL, screen, &current);
    }
}
